from entity import*
from player import*
from QuadTree import*
from camera import*
from platform import*
from companion import*
from moveable import*
from trigger import*

class entitymanager(object):
    """Keeps lists of entities and controls them. Also provides resources that entities may want to access."""
    def __init__(self):
        self.entities = [] #All entities
        self.updateEntities = [] #Entities that need to be updated
        self.keysdown = {}
        self.quadtrees = {1:QuadTree(0, 0, 500, 500, True)}
        self.quadtreeLayers = [1]
        self.layerParallax = {0:[1.5, 1], 1:[1,1], 2:[.95, .95]}
        self.editor = 0
        self.textures = {}

    def addEntity(self, entity, update = False):
        """Adds an entity to be managed, specificy third paramter as true if this entity updates"""
        if(not self.quadtrees.has_key(int(entity.layer))):
            self.quadtrees[int(entity.layer)] = QuadTree(0, 0, 500, 500, True)
            self.quadtreeLayers.append(int(entity.layer))
            self.quadtreeLayers.sort()

        self.quadtrees[int(entity.layer)].insertEntity(entity)
        self.entities.append(entity)
        if(update):
            self.updateEntities.append(entity)

    def removeEntity(self, entity, update = False):
        """Remove entity. Specify third parameter as true if this entity updates"""
        self.quadtrees[int(entity.layer)].removeEntity(entity)
        self.entities.remove(entity)
        if(update):
            self.updateEntities.remove(entity)

    def updateInQuadTree(self, entity):
        self.quadtree.removeEntity(entity)
        self.quadtree.insertEntity(entity)
    
    def getEntitiesNearby(self, rect, layer = 1):
        """Gets entities close enough to possibly collide"""
        return self.quadtrees[layer].getEntitiesNearby(rect)

    def update(self, timeElapsed, keysdown):

        self.keysdown = keysdown
        if(not self.editor.inEditMode):
            for entity in self.updateEntities:
                entity.update(timeElapsed, self)

    def draw(self, camera, screen):
        entities = []
        for num in (self.quadtreeLayers):
            toExtend = self.quadtrees[num].getEntitiesNearby(pygame.Rect(camera.rect[0] * self.layerParallax[num][0],camera.rect[1] * self.layerParallax[num][1], camera.rect[2], camera.rect[3]) )
            entities.extend(toExtend)

        drawList = {}
        for en in entities:
            if(not drawList.has_key(en.layer)):
                drawList[en.layer] = []
            drawList[en.layer].append(en)
        for key in reversed(drawList.keys()):
            for en in drawList[key]:
                en.draw(camera, screen, self)
       # self.quadtrees[1].draw(camera, screen) 
     
    def load(self, entities):
        self.entities[:] = []
        for en in entities:
            en['class'].onLoad(self, en)
            
            
       

